<?php
/**
 * Created by PhpStorm.
 * User: windon
 * Date: 2018/6/9
 * Time: 15:42
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class MediaHistory extends Model
{
    protected $table = 'media_history';

    /**
     * 添加媒体观看记录
     * @param $id
     * @param $uid
     * @param $ticket
     */
    static function addMediaHistory($id, $ticket)
    {
        if($ticket) {
            $where = array(
                ['ticket', '=', $ticket],
                ['status', '=', 1]
            );
            $uid = D('user_tickets')
                ->where($where)
                ->value('uid');
            if (!$uid) {
                EE('当前用户未登录');
            }
            $where_mediahistory = [
                'uid'=>$uid,
                'media_id'=>$id
            ];
            $res_mediahistory = D('media_history')
                ->where($where_mediahistory)
                ->first();
            if(!$res_mediahistory) {
                $where_insert = [
                    'uid' => $uid,
                    'media_id' => $id,
                    'sorts' => 0,
                    'status' => 1,
                    'pv' => 1,
                    'remarks' => '',
                    'created_at' => time2date(),
                    'updated_at' => time2date()
                ];
                D('media_history')->insert($where_insert);
            }else{
                D('media_history')
                    ->where($where_mediahistory)
                    ->increment('pv', 1, ['updated_at' => time2date()]);
            }
        }
    }
}